package gugga.tasks
{
	import flash.events.EventDispatcher;
	import gugga.tasks.events.TaskEvent;
	
	[Event(name="start", type="TaskEvent")]
	[Event(name="interrupted", type="TaskEvent")]
	[Event(name="completed", type="TaskEvent")]
	
	/**
	 * <code>CustomCompleteTask</code> is usefull in cases when we need control over the 
	 * completion of a task. The <b><i>start</i></b>, <b><i>interrupted</i></b> and 
	 * <b><i>completed</i></b> events are dispatched only when the methods <code>start()</code>, 
	 * <code>interrupt()</code> and <code>complete()</code> respectively are called explicitly.
	 */
	public class CustomCompleteTask extends EventDispatcher implements ITask
	{
		private var mIsRunning : Boolean = false;
		
		public function start() : void 
		{
			mIsRunning = true;
			dispatchEvent(new TaskEvent(TaskEvent.START));
		}
		
		public function complete() : void 
		{
			mIsRunning = false;
			dispatchEvent(new TaskEvent(TaskEvent.COMPLETED));
		}
	
		public function isRunning() : Boolean 
		{
			return mIsRunning;
		}
	
		public function isImmediatelyInterruptable() : Boolean 
		{
			return true;
		}
	
		public function interrupt() : void 
		{
			mIsRunning = false;
			dispatchEvent(new TaskEvent(TaskEvent.INTERRUPTED));
		}
	}
}